#include <QApplication>
#include <QCoreApplication>
#include <QSystemSemaphore>
#include <QSharedMemory>
#include <QSettings>
#include <QTranslator>
#include <QMessageBox>
#include <QFileDialog>
#include <QTime>
#include <QDebug>
#include <QWidget>


#include "SettingManager.h"
#include "VarSetManager.h"
//#include "DICOMUtilityTool.h"

#include "widget.h"



static QSharedMemory checkSMem2("OctViewerCKMem");

bool isAppRunning()
{
bool res;

#ifndef Q_OS_WIN
    //in uinix or linux must clear share mem1

    if(checkSMem1.attach()){
        checkSMem1.detach();
    }
#endif
     if(checkSMem2.attach()){
         res=true;
     }else{
         checkSMem2.create(1);
         res=false;
     }
return  res;
}

void loadTranslateFromVarList(QApplication & a,const QVariantList &list);


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSystemSemaphore semaphore("OctViewerRuningCheck", 1);
    checkSMem2.setKey("OctViewerCKMem");
    semaphore.acquire();
    if(checkSMem2.isAttached()) checkSMem2.detach();
    bool isRuning=isAppRunning();
    semaphore.release();

    if(isRuning){
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText(msgBox.tr("The application is already running.\n"));
        msgBox.exec();
        return 1;
    }

#ifdef QT_NO_DEBUG

#else

#endif
    //query translate file and load
    QVariantList *varlist=new QVariantList();
    OCTViewr2::SettingManager::getTranslateFileList(*varlist);
    loadTranslateFromVarList(a,*varlist);

    delete varlist;

    //create settingmanager for read language param
    OCTViewr2::SettingManager *s=new OCTViewr2::SettingManager(nullptr);

    //create varset manager
    OCTViewr2::VarSetManager *v=new OCTViewr2::VarSetManager(nullptr);
    QObject::connect(v,SIGNAL(toastInfo(QString , QString)),
                     s,SLOT(toastInfo(QString , QString))
                     );
    v->initVarSetManagerMisc();

//    //create dicom utility tool instance
//    OCTViewr2::DicomUtilityTool *dicomTool=new OCTViewr2::DicomUtilityTool();
//    dicomTool->setSettingManagerPtr(s);
//    dicomTool->initByGUIThread();

    // exit code
    int res=0;

    Widget* w=nullptr;

    //exec general login dialog
    //s->execGeneralLoginUi();

    w=new Widget;
//    w->showNormal();
    w->setSettingManagerPtr(s);
    w->setVarSetManagerPtr(v);
    w->doShowMisc();
    res=a.exec();
    delete w;

#ifdef QT_NO_DEBUG
    if(s->getGeneralLoginDlgResult()==QDialog::Accepted){
#endif
//        qDebug()<<s->getGeneralLoginLevel();
//        qDebug()<<s->getGeneralLoginID();
//        qDebug()<<s->getGeneralLoginName();

//        dicomTool->setPerformingPhysicianInfo(
//                    s->getGeneralLoginID(),s->getGeneralLoginName()
//                    );

    s->onAppAccountLevelChanged(
                s->getGeneralLoginLevel()
                );

#ifdef QT_NO_DEBUG
    }
#endif

    delete v;
    delete s;
//    delete dicomTool;
    return res;
}

void loadTranslateFromVarList(QApplication & app, const QVariantList &list){
    QTranslator *translator=nullptr;
    int cnt=list.count();
    QString filestr;
    for (int i = 0; i < cnt; ++i) {
        filestr=list[i].toString();
        if(!filestr.isEmpty()){
            translator=new QTranslator();
            translator->load(filestr);
            if(!translator->isEmpty()){
                app.installTranslator(translator);
            }else{
                if(translator) delete translator;
            }
        }
    }
}
